package com.ByteDance.controller;


import com.ByteDance.model.po.UserInfos;
import com.ByteDance.model.po.VideoEntity;
import com.ByteDance.model.vo.FavoriteVO;
import com.ByteDance.model.vo.UserInfosVO;
import com.ByteDance.model.vo.Video;
import com.ByteDance.service.IFavoriteService;
import com.ByteDance.service.IUserService;
import com.ByteDance.service.IVideoService;
import com.ByteDance.utils.JwtUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.libfreenect2.Logger;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 视频点赞表 前端控制器
 * </p>
 *
 * @author 朱哥
 * @since 2024-03-03
 */
@Slf4j
@Api(tags="FavoriteController")
@RestController
@RequestMapping("douyin/favorite")
@RequiredArgsConstructor
public class FavoriteController {
    private final IFavoriteService iFavoriteService;
    private final IVideoService iVideoService;
    private final IUserService iUserService;
    @ApiOperation("赞操作接口")
    @PostMapping("/action")
    public FavoriteVO action(@RequestParam(value = "video_id")int videoId, @RequestParam(value = "action_type")int actionType, HttpServletRequest req){
        VideoEntity videoEntity = iVideoService.getById(videoId);
        if (videoEntity == null){
            return FavoriteVO.fail();
        }
        int userId = JwtUtils.getUserId(req.getHeader("token"));//获取userId到时候保存信息的时候用
        Boolean flag = iFavoriteService.action(userId,videoId,actionType);
        if(flag){
            log.info("赞操作成功");
            return FavoriteVO.success();
        }else{
            log.info("赞操作失败");
            return FavoriteVO.fail();
        }
    }

     /**获取登录用户的所有点赞视频
     * @param userId
     * @return
     */
    @ApiOperation("喜欢列表接口")
    @GetMapping("/list")
    public FavoriteVO list(@RequestParam(value = "user_id")int userId){
        UserInfos userInfos = iUserService.getById(userId);
        if(userInfos == null){
            return FavoriteVO.fail();
        }
        Video[] videos = iFavoriteService.searchFavoriteByUserId(userId);
        FavoriteVO favoriteVO = FavoriteVO.success();
        favoriteVO.setVideoList(videos);
        return favoriteVO;
    }
}
